/*
 * Created on 06.02.2005
 * 
 */
package soccerscope.daistatistic;

import java.util.Vector;

import robocup.component.RobocupAgent;
import robocup.component.StateLoader;
import soccerscope.SoccerScope;
import soccerscope.SoccerScopeMenuBar;
import soccerscope.net.AgentConnector;

/**
 * @author Andr�
 * 
 * class to register the playerbeans, the tactics and the coach so they can be
 * used by the gui
 */
public class Register {

  static int                       registeredPlayers = 0;

  static Vector<RobocupAgent>      playerBeans       = new Vector<RobocupAgent>();

  static Vector<StateLoader>       tactics           = new Vector<StateLoader>();

  public static SoccerScopeMenuBar scopeMenu;

  public static SoccerScope        soccerScope       = null;

  public static boolean            saveDaiWorldModel = true;

  /**
   * 
   * register a PlayerBean
   * 
   * @param PlayerBean
   *          bean
   */
  public static void registerPlayerBean(RobocupAgent bean) {

    Register.registeredPlayers++;
    Register.playerBeans.add(bean);

  }

  /**
   * 
   * register a TacticBean
   * 
   * @param TacticBean
   *          bean
   */
  public static void registerTactic(StateLoader tactic) {

    Register.tactics.add(tactic);
  }

  /**
   * get the playerbean of a specified player
   * 
   * @param playerNumber
   *          the playernumber
   */
  public static RobocupAgent getPlayerBeanWithPlayerNumber(int playerNumber) {

    for (RobocupAgent pBean : Register.playerBeans) {
      if (pBean.getWorld().getNumber() == playerNumber) {
        if (pBean.getTeamName() != null) {
          if (pBean.getTeamName().equals(
              Register.playerBeans.firstElement().getTeamName())) {
            // players
            // from
            // the
            // first
            // registered
            // team
            return pBean;
          }
        }
      }
    }
    return null;
  }

  /**
   * 
   * @return
   */
  public synchronized static int getNumberOfPlayersLeftTeam() {

    int num = 0;

    synchronized (Register.playerBeans) {
      for (RobocupAgent pBean : Register.playerBeans) {
        if (pBean != null && Register.playerBeans.firstElement() != null) {
          if (pBean.getTeamName() != null && Register.playerBeans
              .firstElement().getTeamName() != null) {
            if (pBean.getTeamName().equals(
                Register.playerBeans.firstElement().getTeamName())) {
              num++;

            }
            else {
              if (pBean.getTeamName() == null && num < 11) {
                num++;
              }
            }
          }
        }
      }
    }
    return num;
  }

  /**
   * get the tactic of an specified player
   * 
   * @param int
   *          playerNumber the playernumber
   */
  public static StateLoader getTacticOfPlayer(int playerNumber) {

    for (StateLoader tBean : Register.tactics) {
      if (tBean.getAgent().getWorld().getNumber() == playerNumber) {
        return tBean;
      }
    }
    return null;
  }

  /**
   * updates the dai history of a player
   * 
   * @param pBean
   */
  public static void infoReceived(RobocupAgent pBean) {

    if (Register.saveDaiWorldModel) {
      if (pBean != null && pBean.getNumber() > 0 && pBean.getWorld() != null && pBean
          .getWorld().getLongTermMemory() != null) {
        if (pBean.getTeamName().equals(
            Register.playerBeans.firstElement().getTeamName())) {
          if (pBean.getWorld().getBallRef() != null) {
            if (pBean.getWorld().getBallRef().getPosition() != null) {
              AgentConnector.addData(pBean, Register.scopeMenu);// getPlayerBeanWithPlayerNumber(pBean.getMyWorld().getNumber()));
            }
          }
        }
      }
    }

  }

  /**
   * updates player pass
   * 
   * @param shooter
   * @param reciever
   */
  public static void passToTeammate(RobocupAgent shooter,
      int reciever) {

    if (shooter.getTeamName().equals(
        Register.playerBeans.firstElement().getTeamName()) && Register.saveDaiWorldModel) {
      AgentConnector.addPassData(shooter, reciever);
    }
  }

  /**
   * updates player holes
   * 
   * @param playerNumber
   * @param numberOfNewHoles
   */
  public static void hole(RobocupAgent shooter,
      int numberOfNewHoles) {

    AgentConnector.addHoles(shooter, numberOfNewHoles);
    /*
     * if (shooter == null) return; if
     * (shooter.getTeamName().equals(playerBeans.firstElement().getTeamName()) &&
     * saveDaiWorldModel)
     * DaiWorldModelHistory.addHoles(shooter,numberOfNewHoles);
     */

  }

  // /**
  // * add the performed state to history
  // *
  // * @param state,
  // * the performed state
  // * @param wm,
  // * the player worldmodel
  // */
  // public static void addState(String state, WorldModel wm) {
  // DaiWorldModelHistory.addState(wm.getCycle(), wm.getNumber(), state);
  // }

  /**
   * add state assessements to history
   * 
   * @param stateType,
   *          the state for the assessment
   * @param ass,
   *          the assessment
   * @param wm,
   *          the player worldmodel
   */
  public static void addStateAss(String stateInfo,
      RobocupAgent agent) {

    AgentConnector.addStateAss(agent, stateInfo);
  }
}
/**
 * $Log: Register.java,v $ Revision 1.4 2006/11/05 13:58:04 wetzker more merging -
 * CLang + MF + some states still missing
 * 
 * Revision 1.3.26.4 2006/10/25 14:33:38 endert Latest SoccerScope Version plus
 * Database ability
 * 
 * Revision 1.3.12.2 2006/07/31 13:48:58 benjaboe Connection from Agent is named
 * comprehensible
 * 
 * Revision 1.3.12.1 2006/07/31 13:20:08 benjaboe States of the agents is shown
 * on the right side too
 * 
 * Revision 1.3 2006/03/27 23:25:53 wetzker simplified stateEvaluation - moved
 * most of SimpleState into Abstract - moved most of StateEvaluation into
 * StateTactic adapted GUIUpdate - deleted state tables - only 1 state update
 * each cycle - gui should now be much faster (less holes)
 * 
 * Revision 1.2 2006/03/22 23:38:20 wetzker cleaned gui connection - wm is now
 * only send after an action - added more say + attentionTo logic set dribble
 * state value -20 percent for nightly tournament added state memory to LTM
 * 
 * Revision 1.1 2006/03/13 07:38:28 axle moved from ROOT/src for Maven 2
 * Buildsystem and Team/Soccerscope separation
 * 
 * Revision 1.24 2006/02/22 16:05:09 endert This is done: - Rebound SoccerScope
 * to Agents (can be added or removed in config-file) - Situations now all
 * extend abstract class Situation - Refactorings: ActionBean _ removed from
 * Attributes - SoccerScope will automatically connect to host given in
 * Start-Call - Trainer uses the same host as soccerscope - Removed unused stuff
 * (old confs, bat, etc.) - Code formatted (good luck when merging) - organized
 * imports - all that i forgot to mention here Revision 1.23 2006/02/06 17:09:46
 * endert New Communication merged, bla bla
 * 
 * Revision 1.22.36.1 2005/12/27 19:36:03 endert New Branch created for my Dipl!
 * 
 * Revision 1.22 2005/10/03 11:49:39 andpaul documentation update
 * 
 * Revision 1.21 2005/10/02 17:02:13 andpaul added: state value statistic: per
 * player overview
 * 
 * Revision 1.20 2005/09/27 13:11:35 andpaul updated: state statistic/assessment
 * 
 * Revision 1.19 2005/09/26 22:18:34 andpaul updated: state statistic/values
 * 
 * Revision 1.18 2005/09/01 08:29:08 andpaul added: showing actual player state
 * in WorldModelLayer (on the right site of the playing field)
 * 
 * Revision 1.17 2005/08/16 16:26:38 wetzker merged composite branch
 * 
 * Revision 1.15.2.2 2005/08/16 15:43:29 wetzker removed ball and player cloning
 * in getSelf, getBall -> should be much faster now merged with changes from
 * head
 * 
 * Revision 1.16 2005/08/03 08:11:39 andpaul added: showing info type in
 * WorldModelLayer
 * 
 * Revision 1.15 2005/06/22 10:42:48 andpaul changed: added entry for holes in
 * cycle 0
 * 
 * Revision 1.14 2005/06/20 17:30:39 andpaul documentation
 * 
 * Revision 1.13 2005/06/15 17:03:31 wetzker removed JIAC from Head + changes in
 * framework please report bugs or shortcomings some scenarios might not work
 * because of wrong imports
 * 
 * Revision 1.11.2.2 2005/06/15 16:44:54 wetzker final version
 * 
 * Revision 1.11.2.1 2005/06/09 12:32:07 endert Removal of JIAC Libraries and
 * dependencies from project, restructuring the framework and improve some
 * aspects (hoping it is an improvement)
 * 
 * Revision 1.12 2005/06/13 12:02:40 andpaul added: reference to soccer scope
 * 
 * Revision 1.11 2005/06/05 09:47:09 andpaul added: values to store are
 * selectable via Dai MenuItem
 * 
 * Revision 1.10 2005/05/16 14:24:00 andpaul NPE hotfix - i hope so (in
 * getPlayerBeanWithPlayerNumber)
 * 
 * Revision 1.9 2005/05/15 12:27:26 haeder small change in
 * getNumberOfPlayersLeftTeam() to prevent NPE
 * 
 * Revision 1.8 2005/05/13 22:20:56 wetzker refactoring in LongTermMemory
 * renamed some strange methods gui-statistic is not updated with constant cycle
 * as everything jumps when PM changes turned off some (strange looking)
 * if-cases in GUI-Statistics MeModel now contains viewWidth and viewQuality
 * 
 * Revision 1.7 2005/05/12 17:06:10 wetzker removed bug which leaded to not
 * showing the statistics of number 1 removed yesterdays synchro changes
 * 
 * Revision 1.6 2005/05/09 19:20:00 andpaul added: methods to recieve holes and
 * pass informations
 * 
 * Revision 1.5 2005/05/05 11:48:02 haeder removed unused local variable
 * 
 * Revision 1.4 2005/05/02 18:27:24 till outsourced FactBase to LongTermMemory
 * 
 * Revision 1.3 2005/04/25 15:21:31 andpaul added: selector for dai wm recording
 * in DAI menu
 * 
 * Revision 1.2 2005/04/25 14:53:16 andpaul - removed: coach registration - not
 * needed anymore - added: inform gui when visual info was recieved
 * 
 * Revision 1.1 2005/04/20 10:39:38 endert Fresh Team Repository installation
 * from /home/e/endert/Robocup to here.
 * 
 * Revision 1.6 2005/04/12 12:38:47 andpaul time again for code documentation -
 * same.Chart; same.XYDatasets; PosDifChart;StaminaChart and ViewAngleChart are
 * depricated. Will deleted if statistic dialog layout is final.
 * 
 * Revision 1.5 2005/03/22 16:30:55 andpaul some minor changes added a little
 * error handling
 * 
 * Revision 1.0 2005/01/30 17:20:42 andpaul setting up the register class
 * 
 * 
 * 
 * 
 */
